package o;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInstaller.Session;
import android.content.pm.PackageInstaller.SessionParams;
import android.net.Uri;
import android.os.Process;
import android.text.TextUtils;
import androidx.annotation.Nullable;
import com.huawei.appgallery.datastorage.provider.CommonFileProvider;
import com.huawei.appgallery.packagemanager.api.bean.ManagerTask;
import com.huawei.appgallery.packagemanager.api.bean.ProfileTask;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public abstract class cpt {
    private static boolean ˋ = false;

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public static int ॱ(android.content.Context r11, com.huawei.appgallery.packagemanager.api.bean.ManagerTask r12, boolean r13) {
        /*
        r2 = 1;
        r1 = 0;
        r4 = ˏ(r12, r13);
        r0 = r11.getPackageManager();
        r0 = r0.getPackageInstaller();
        r3 = 0;
        r5 = r0.createSession(r4);	 Catch:{ IOException -> 0x014b, all -> 0x0139 }
        r6 = 65536; // 0x10000 float:9.18355E-41 double:3.2379E-319;
        r6 = new byte[r6];	 Catch:{ IOException -> 0x014b, all -> 0x0139 }
        r3 = r0.openSession(r5);	 Catch:{ IOException -> 0x014b, all -> 0x0139 }
        r0 = r12.apkInfos;	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r7 = r0.iterator();	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
    L_0x0021:
        r0 = r7.hasNext();	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        if (r0 == 0) goto L_0x00c4;
    L_0x0027:
        r0 = r7.next();	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r0 = (o.cof.a) r0;	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r8 = new java.io.File;	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r0 = r0.ˎ;	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r8.<init>(r0);	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r0 = r8.exists();	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        if (r0 != 0) goto L_0x0064;
    L_0x003a:
        r0 = o.cnu.ˏ;	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r2 = "InstallProcessSdk24";
        r4 = "app file not exists";
        r0.ˊ(r2, r4);	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r0 = o.cpa.ˊ();	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r2 = r12.packageName;	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r0.ˎ(r11, r2);	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r0 = o.cop.ॱ();	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r2 = 4;
        r0 = r0.ˏ(r12, r2);	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        if (r0 == 0) goto L_0x005a;
    L_0x0057:
        o.cpq.ˋ(r12);	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
    L_0x005a:
        r3.abandon();	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        if (r3 == 0) goto L_0x0062;
    L_0x005f:
        r3.close();
    L_0x0062:
        r0 = r1;
    L_0x0063:
        return r0;
    L_0x0064:
        r0 = ˋ(r11, r4, r8, r3, r6);	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        if (r0 == r2) goto L_0x0070;
    L_0x006a:
        if (r3 == 0) goto L_0x0063;
    L_0x006c:
        r3.close();
        goto L_0x0063;
    L_0x0070:
        r0 = o.cnu.ˏ;	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r8 = "InstallProcessSdk24";
        r9 = new java.lang.StringBuilder;	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r9.<init>();	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r10 = "innerInstall fsync file end:";
        r9 = r9.append(r10);	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r10 = r12.packageName;	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r9 = r9.append(r10);	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r10 = "-";
        r9 = r9.append(r10);	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r9 = r9.append(r5);	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r9 = r9.toString();	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r0.ˋ(r8, r9);	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        goto L_0x0021;
    L_0x0097:
        r0 = move-exception;
        r2 = r3;
    L_0x0099:
        r3 = o.cnu.ˏ;	 Catch:{ all -> 0x0148 }
        r4 = "InstallProcessSdk24";
        r5 = new java.lang.StringBuilder;	 Catch:{ all -> 0x0148 }
        r5.<init>();	 Catch:{ all -> 0x0148 }
        r6 = "IOException";
        r5 = r5.append(r6);	 Catch:{ all -> 0x0148 }
        r6 = r0.toString();	 Catch:{ all -> 0x0148 }
        r5 = r5.append(r6);	 Catch:{ all -> 0x0148 }
        r5 = r5.toString();	 Catch:{ all -> 0x0148 }
        r3.ˊ(r4, r5);	 Catch:{ all -> 0x0148 }
        r0 = ॱ(r0);	 Catch:{ all -> 0x0148 }
        if (r0 == 0) goto L_0x0140;
    L_0x00bd:
        if (r2 == 0) goto L_0x00c2;
    L_0x00bf:
        r2.close();
    L_0x00c2:
        r0 = r1;
        goto L_0x0063;
    L_0x00c4:
        r0 = r12.ˎ;	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        if (r0 != 0) goto L_0x00d8;
    L_0x00c8:
        r0 = new o.coo;	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r0.<init>();	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r4 = r12.packageName;	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r7 = r12.versionCode;	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r0 = r0.ˊ(r11, r4, r7);	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        ˏ(r11, r3, r6, r0);	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
    L_0x00d8:
        r0 = new android.content.Intent;	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r4 = com.huawei.appgallery.packagemanager.impl.install.control.InstallResultReceiver.class;
        r0.<init>(r11, r4);	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r4 = "package_task_result_receiver_packagename";
        r6 = r12.packageName;	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r0.putExtra(r4, r6);	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r4 = "package_task_result_receiver_taskid";
        r6 = r12.taskId;	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r0.putExtra(r4, r6);	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r4 = 134217728; // 0x8000000 float:3.85186E-34 double:6.63123685E-316;
        r0 = android.app.PendingIntent.getBroadcast(r11, r5, r0, r4);	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        if (r0 == 0) goto L_0x011e;
    L_0x00f5:
        r0 = r0.getIntentSender();	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r3.commit(r0);	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r0 = o.cnu.ˏ;	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r4 = "InstallProcessSdk24";
        r6 = new java.lang.StringBuilder;	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r6.<init>();	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r7 = "session commit ";
        r6 = r6.append(r7);	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r5 = r6.append(r5);	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r5 = r5.toString();	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r0.ˋ(r4, r5);	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
    L_0x0116:
        if (r3 == 0) goto L_0x011b;
    L_0x0118:
        r3.close();
    L_0x011b:
        r0 = r2;
        goto L_0x0063;
    L_0x011e:
        r0 = o.cnu.ˏ;	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r4 = "InstallProcessSdk24";
        r6 = new java.lang.StringBuilder;	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r6.<init>();	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r7 = "No broadcast found ";
        r6 = r6.append(r7);	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r5 = r6.append(r5);	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r5 = r5.toString();	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        r0.ˊ(r4, r5);	 Catch:{ IOException -> 0x0097, all -> 0x0139 }
        goto L_0x0116;
    L_0x0139:
        r0 = move-exception;
    L_0x013a:
        if (r3 == 0) goto L_0x013f;
    L_0x013c:
        r3.close();
    L_0x013f:
        throw r0;
    L_0x0140:
        r0 = 2;
        if (r2 == 0) goto L_0x0063;
    L_0x0143:
        r2.close();
        goto L_0x0063;
    L_0x0148:
        r0 = move-exception;
        r3 = r2;
        goto L_0x013a;
    L_0x014b:
        r0 = move-exception;
        r2 = r3;
        goto L_0x0099;
        */
        throw new UnsupportedOperationException("Method not decompiled: o.cpt.ॱ(android.content.Context, com.huawei.appgallery.packagemanager.api.bean.ManagerTask, boolean):int");
    }

    private static SessionParams ˏ(ManagerTask managerTask, boolean z) {
        ReflectiveOperationException e;
        SessionParams sessionParams = new SessionParams(managerTask.mode);
        sessionParams.setAppPackageName(managerTask.packageName);
        ˊ(sessionParams, z);
        try {
            SessionParams.class.getDeclaredField("originatingUid").setInt(sessionParams, Process.myUid());
        } catch (NoSuchFieldException e2) {
            e = e2;
            cnu.ˏ.ˋ("InstallProcessSdk24", "can not set originatingUid:" + e.toString());
            if (managerTask.mode == 2) {
                cos.ॱ(sessionParams);
            }
            return sessionParams;
        } catch (IllegalAccessException e3) {
            e = e3;
            cnu.ˏ.ˋ("InstallProcessSdk24", "can not set originatingUid:" + e.toString());
            if (managerTask.mode == 2) {
                cos.ॱ(sessionParams);
            }
            return sessionParams;
        }
        if (managerTask.mode == 2) {
            cos.ॱ(sessionParams);
        }
        return sessionParams;
    }

    @TargetApi(21)
    protected static int ˋ(Context context, SessionParams sessionParams, File file, Session session, byte[] bArr) {
        Closeable fileInputStream;
        Closeable openWrite;
        Throwable e;
        Closeable closeable = null;
        try {
            fileInputStream = new FileInputStream(file);
            try {
                long length = file.length();
                try {
                    openWrite = session.openWrite(file.getName(), 0, length);
                    try {
                        Method ˋ = ˋ();
                        while (true) {
                            int read = fileInputStream.read(bArr);
                            if (read != -1) {
                                openWrite.write(bArr, 0, read);
                                ॱ(session, length, ˋ, (float) read);
                            } else {
                                session.fsync(openWrite);
                                ˎ(openWrite);
                                ˎ(fileInputStream);
                                return 1;
                            }
                        }
                    } catch (IOException e2) {
                        e = e2;
                        closeable = fileInputStream;
                        try {
                            cnu.ˏ.ˊ("InstallProcessSdk24", "write apk failed", e);
                            session.abandon();
                            ˎ(openWrite);
                            ˎ(closeable);
                            return 0;
                        } catch (Throwable th) {
                            e = th;
                            fileInputStream = closeable;
                            ˎ(openWrite);
                            ˎ(fileInputStream);
                            throw e;
                        }
                    } catch (Throwable th2) {
                        e = th2;
                        ˎ(openWrite);
                        ˎ(fileInputStream);
                        throw e;
                    }
                } catch (SecurityException e3) {
                    session.abandon();
                    int ˎ = ˎ(sessionParams, e3);
                    ˎ(null);
                    ˎ(fileInputStream);
                    return ˎ;
                }
            } catch (IOException e4) {
                e = e4;
                openWrite = null;
                closeable = fileInputStream;
                cnu.ˏ.ˊ("InstallProcessSdk24", "write apk failed", e);
                session.abandon();
                ˎ(openWrite);
                ˎ(closeable);
                return 0;
            } catch (Throwable th3) {
                e = th3;
                openWrite = null;
                ˎ(openWrite);
                ˎ(fileInputStream);
                throw e;
            }
        } catch (IOException e5) {
            e = e5;
            openWrite = null;
            cnu.ˏ.ˊ("InstallProcessSdk24", "write apk failed", e);
            session.abandon();
            ˎ(openWrite);
            ˎ(closeable);
            return 0;
        } catch (Throwable th4) {
            e = th4;
            openWrite = null;
            fileInputStream = null;
            ˎ(openWrite);
            ˎ(fileInputStream);
            throw e;
        }
    }

    private static int ˎ(SessionParams sessionParams, SecurityException securityException) {
        if (ˋ(sessionParams)) {
            cnu.ˏ.ˋ("InstallProcessSdk24", "can not set ALLOCATE_AGGRESSIVE,do install again.");
            return -2;
        }
        cnu.ˏ.ˊ("InstallProcessSdk24", "can not openWrite", securityException);
        return 0;
    }

    private static boolean ˋ(SessionParams sessionParams) {
        int i;
        try {
            i = sessionParams.getClass().getField("installFlags").getInt(sessionParams);
        } catch (NoSuchFieldException e) {
            cnu.ˏ.ˋ("InstallProcessSdk24", "can not get installFlags" + e.getMessage());
            i = 0;
        } catch (IllegalAccessException e2) {
            cnu.ˏ.ˋ("InstallProcessSdk24", "can not get installFlags" + e2.getMessage());
            i = 0;
        }
        if ((i & 32768) != 0) {
            return true;
        }
        return false;
    }

    private static void ˊ(SessionParams sessionParams, boolean z) {
        try {
            sessionParams.getClass().getMethod("setAllocateAggressive", new Class[]{Boolean.TYPE}).invoke(sessionParams, new Object[]{Boolean.valueOf(z)});
        } catch (NoSuchMethodException e) {
            cnu.ˏ.ˏ("InstallProcessSdk24", "can not setAllocateAggressive" + e.getMessage());
        } catch (IllegalAccessException e2) {
            cnu.ˏ.ˏ("InstallProcessSdk24", "can not setAllocateAggressive" + e2.getMessage());
        } catch (InvocationTargetException e3) {
            cnu.ˏ.ˏ("InstallProcessSdk24", "can not setAllocateAggressive" + e3.getMessage());
        } catch (Throwable th) {
            cnu.ˏ.ˏ("InstallProcessSdk24", "can not setAllocateAggressive" + th.getMessage());
        }
    }

    private static boolean ॱ(Exception exception) {
        if ((exception instanceof IOException) && exception.getStackTrace() != null) {
            for (StackTraceElement fileName : exception.getStackTrace()) {
                if ("ParcelableException.java".equals(fileName.getFileName())) {
                    return false;
                }
            }
        }
        return true;
    }

    private static void ˎ(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException e) {
                cnu.ˏ.ˊ("InstallProcessSdk24", e.toString());
            }
        }
    }

    private static void ॱ(Context context) {
        if (!ˋ) {
            bmo o_bmo = new bmo();
            o_bmo.ˏ = new File(context.getFilesDir(), "/");
            o_bmo.ˋ = ".apk";
            CommonFileProvider.ˏ("installfile", o_bmo);
        }
    }

    @Nullable
    public static Intent ˎ(Context context, String str) {
        ॱ(context);
        Uri ˋ = CommonFileProvider.ˋ(context, new File(str));
        if (ˋ == null) {
            cnu.ˏ.ˊ("InstallProcessSdk24", "error filePath");
            return null;
        }
        Intent data = new Intent("android.intent.action.INSTALL_PACKAGE").setData(ˋ);
        if (!(context instanceof Activity)) {
            data.addFlags(268435456);
        }
        data.addFlags(1);
        return data;
    }

    @TargetApi(21)
    private static void ˏ(Context context, Session session, byte[] bArr, ProfileTask profileTask) {
        Closeable openWrite;
        Throwable e;
        Closeable closeable = null;
        if (profileTask != null && new coo().ˊ(context) && !TextUtils.isEmpty(profileTask.profilePath)) {
            File file = new File(profileTask.profilePath);
            if (file.exists()) {
                cnu.ˏ.ˋ("InstallProcessSdk24", "begin write profile:" + profileTask.packageName);
                Closeable fileInputStream;
                try {
                    fileInputStream = new FileInputStream(file);
                    try {
                        openWrite = session.openWrite("base.dm", 0, -1);
                        while (true) {
                            try {
                                int read = fileInputStream.read(bArr);
                                if (read != -1) {
                                    openWrite.write(bArr, 0, read);
                                } else {
                                    session.fsync(openWrite);
                                    Intent intent = new Intent("AppProfile.CommitResult");
                                    intent.putExtra("AppProfile.DownloadResult.Param.PackageName", profileTask.packageName);
                                    intent.putExtra("AppProfile.DownloadResult.Param.VersionCode", profileTask.versionCode);
                                    ll.ˏ(context).ˋ(intent);
                                    ˎ(openWrite);
                                    ˎ(fileInputStream);
                                    cnu.ˏ.ˋ("InstallProcessSdk24", "end write profile:" + profileTask.packageName);
                                    return;
                                }
                            } catch (IOException e2) {
                                e = e2;
                                closeable = fileInputStream;
                            } catch (Throwable th) {
                                e = th;
                            }
                        }
                    } catch (IOException e3) {
                        e = e3;
                        openWrite = null;
                        closeable = fileInputStream;
                        try {
                            cnu.ˏ.ˊ("InstallProcessSdk24", "write profile failed:" + profileTask.packageName, e);
                            ˎ(openWrite);
                            ˎ(closeable);
                            cnu.ˏ.ˋ("InstallProcessSdk24", "end write profile:" + profileTask.packageName);
                        } catch (Throwable th2) {
                            e = th2;
                            fileInputStream = closeable;
                            ˎ(openWrite);
                            ˎ(fileInputStream);
                            cnu.ˏ.ˋ("InstallProcessSdk24", "end write profile:" + profileTask.packageName);
                            throw e;
                        }
                    } catch (Throwable th3) {
                        e = th3;
                        openWrite = null;
                        ˎ(openWrite);
                        ˎ(fileInputStream);
                        cnu.ˏ.ˋ("InstallProcessSdk24", "end write profile:" + profileTask.packageName);
                        throw e;
                    }
                } catch (IOException e4) {
                    e = e4;
                    openWrite = null;
                    cnu.ˏ.ˊ("InstallProcessSdk24", "write profile failed:" + profileTask.packageName, e);
                    ˎ(openWrite);
                    ˎ(closeable);
                    cnu.ˏ.ˋ("InstallProcessSdk24", "end write profile:" + profileTask.packageName);
                } catch (Throwable th4) {
                    e = th4;
                    openWrite = null;
                    fileInputStream = null;
                    ˎ(openWrite);
                    ˎ(fileInputStream);
                    cnu.ˏ.ˋ("InstallProcessSdk24", "end write profile:" + profileTask.packageName);
                    throw e;
                }
            }
        }
    }

    @TargetApi(21)
    @Nullable
    private static Method ˋ() {
        Method method = null;
        try {
            method = Session.class.getMethod("addProgress", new Class[]{Float.TYPE});
        } catch (Throwable e) {
            cnu.ˏ.ˊ("InstallProcessSdk24", "addProgress", e);
        }
        return method;
    }

    private static void ॱ(Session session, long j, Method method, float f) {
        if (j > 0 && method != null) {
            try {
                method.invoke(session, new Object[]{Float.valueOf(f / ((float) j))});
            } catch (Throwable e) {
                cnu.ˏ.ˊ("InstallProcessSdk24", "invoke", e);
            } catch (Throwable e2) {
                cnu.ˏ.ˊ("InstallProcessSdk24", "invoke", e2);
            }
        }
    }
}
